/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package it.unibo.as.server;

import com.google.gwt.user.server.rpc.RemoteServiceServlet;
import it.unibo.as.SessionBeans.AuthenticationBeanLocal;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ConcurrentMap;
import javax.annotation.Resource;
import javax.ejb.EJB;
import javax.servlet.http.HttpSession;
import javax.sql.DataSource;
import net.zschech.gwt.comet.server.CometServlet;
import net.zschech.gwt.comet.server.CometSession;

import it.unibo.as.client.GWTAccountService;

/**
 *
 * @author Johnny
 */
public class GWTAccountServiceImpl extends RemoteServiceServlet implements GWTAccountService {
    @EJB
    private AuthenticationBeanLocal authenticationBean;

  

    public static ConcurrentMap<String, CometSession> users = new ConcurrentHashMap<String, CometSession>();

    @Override
    public String isLogged(){

	// check if there is a HTTP session setup.
        
	HttpSession httpSession = getThreadLocalRequest().getSession(false);
	if (httpSession == null) {
		return null;
	}

	// return the user name
	return (String) httpSession.getAttribute("user");
        
    }
    
    @Override
    public Boolean login(String user, String password) {
        Boolean valid = Boolean.FALSE;

        valid = authenticationBean.LogInWithPwd(user, password);

        if(valid){
            // Get or create the HTTP session for the browser
            HttpSession httpSession = getThreadLocalRequest().getSession();
            // Get or create the Comet session for the browser
            CometSession cometSession = CometServlet.getCometSession(httpSession);
            // Remember the user name for the
            httpSession.setAttribute("user", user);

            // setup the mapping of user names to CometSessions
            if (users.putIfAbsent(user, cometSession) != null) {
                // some one else has already logged in with this user name
                httpSession.invalidate();
                //TODO EXCEPTION HANDLING throw new ChatException("User: " + username + " already logged in");
            }
        }


        return valid;
    }


    @Override
    public Boolean signUp(String fname, String lname, String nickname, String email, String password){
        return authenticationBean.SignUp(fname, lname, nickname, email, password);
        
    }

    @Override
    public void signout(String username) {
	// check if there is a HTTP session setup.
	HttpSession httpSession = getThreadLocalRequest().getSession(false);
	if (httpSession == null) {
            //TODO throw new ChatException("User: " + username + " is not logged in: no http session");
	}

	// check if there is a Comet session setup. In a larger application the HTTP session may have been
	// setup via other means.
	CometSession cometSession = CometServlet.getCometSession(httpSession, false);
	if (cometSession == null) {
            //TODO throw new ChatException("User: " + username + " is not logged in: no comet session");
	}

        // check the user name parameter matches the HTTP sessions user name
	if (!username.equals(httpSession.getAttribute("username"))) {
            // TODO throw new ChatException("User: " + username + " is not logged in on this session");
	}

	// remove the mapping of user name to CometSession
	users.remove(username, cometSession);
	httpSession.invalidate();
	}




}
